package com.mathcat.mobile.android.net;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * HttpTransactionWaitingList.
 * 
 * @author Mathcat
 * @version 1.1.0, 2011.03.14
 */
class HttpTransactionWaitingList {

	private HashMap<URI, ArrayList<HttpTransaction>> waitingList = new HashMap<URI, ArrayList<HttpTransaction>>();
	
	synchronized boolean contains(URI uri) {
		return this.waitingList.containsKey(uri);
	}
	
	synchronized void add(URI uri, HttpTransaction transaction) {
		ArrayList<HttpTransaction> list = this.waitingList.get(uri);
		if (list == null) {
			list = new ArrayList<HttpTransaction>();
			this.waitingList.put(uri, list);
		}
		list.add(transaction);
	}
	
	synchronized HttpTransaction[] getAllByURI(URI uri) {
		ArrayList<HttpTransaction> list = this.waitingList.remove(uri);
		
		if (list == null)
			return null;
		
		HttpTransaction[] transactions = new HttpTransaction[list.size()];
		list.toArray(transactions);
		return transactions;
	}
}
